<?php


namespace App\Http\Controllers\Admin\Remind;

use App\Enums\ExportEnum;
use Illuminate\Http\Request;

//引用http请求类
use App\Exceptions\BasicException;
use App\Response\Response;
use App\Services\Tools\ExportService;
use \App\Http\Controllers\BaseController;

class RemindController extends BaseController
{
    //留存数据报表

    /**
     * @api {get}  /remain/index 留存数据报表
     * @apiVersion 0.1.0
     * @apiName 留存数据报表
     * @apiGroup 补充接口-订单管理
     * @apiParam {Int}                     page  （选填）
     * @apiParam {Int}                     page_size  （选填）
     * @apiParam {Int}                     start_time 开始时间 秒级时间戳（选填）
     * @apiParam {Int}                     end_time 结束时间（选填）
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "total": 12,
     * "current_page": 1,
     * "last_page": 6,
     * "list": [
     * {
     * "all_user_money": "77371938.92",//平台所有用户总余额
     * "all_user_earnings": "117739853.47",//平台所有用户总收益
     * "all_user_gift": "163095932",//平台所有用户背包礼物价值
     * "all_labor_earnings": "108807",//公会收益
     * "all_labor_money": "1108",//公会余额
     * "all_water_money": 6473000,//总水滴价值
     * "all_game_money": 744000,//霍克游戏钻石价值金币数
     * "all_check_money": "116246000.00"//提现审核中金币数
     * "created_at": "116246000.00"//纪录时间
     * },
     * {
     * "all_user_money": "77599867.92",
     * "all_user_earnings": "117366433.17",
     * "all_user_gift": "165473540",
     * "all_labor_earnings": "108807",
     * "all_labor_money": "1108",
     * "all_water_money": 6359200,
     * "all_game_money": 485600,
     * "all_check_money": "116536000.00"
     * }
     * ]
     * }
     * }
     * @apiErrorExample 错误响应
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     */
    public function remindStat(Request $request)
    {
        $param = $request->only(['page', 'page_size', 'start_time', 'end_time']);
        $va = app('validator')->make($param, [
            'page'       => 'integer|min:1',//
            'page_size'  => 'integer|min:1',//
            'start_time' => 'integer|min:1',//开始时间
            'end_time'   => 'integer|min:1',//结束时间
        ]);
        if ($va->fails()) {
            throw  new BasicException(10001, $va->errors()->first());
        }
        $param['page'] = $param['page'] ?? 1;
        $param['page_size'] = $param['page_size'] ?? 20;
        $where = [];
        if (isset($param['start_time'])) {
            $where[] = ['created_at', '>=', date('Y-m-d H:i:s', $param['start_time'])];
        }
        if (isset($param['end_time'])) {
            $where[] = ['created_at', '<=', date('Y-m-d H:i:s', $param['end_time'])];
        }

        if (empty($where)) {
            $list = app('model_account_remind')
                ->orderBy('id', 'desc')
                ->paginate($param['page_size'], ['*'], '', $param['page']);
        } else {
            $list = app('model_account_remind')
                ->where($where)
                ->orderBy('id', 'desc')
                ->paginate($param['page_size'], ['*'], '', $param['page']);
        }


        $list = empty($list) ? [] : $list->toArray();
        $res = [];

        foreach ($list['data'] as $k => $v) {
            $data = $this->dealData(json_decode($v['origin_data'], true));

            $res[$k] = $data;
            $res[$k]['created_at'] = $v['created_at'];
        }

        $result = [
            'total'        => $list['total'] ?? 0,
            'current_page' => $list['current_page'] ?? 1,
            'last_page'    => $list['last_page'] ?? 1,
            'list'         => $res,
        ];

        return Response::sendData($result);
    }


    /**
     * @api {get}  /remain/export_xls 留存数据导出
     * @apiVersion 0.1.0
     * @apiName 留存数据报表
     * @apiGroup 补充接口-订单管理
     * @apiParam {Int}                     start_time 开始时间 秒级时间戳（选填）
     * @apiParam {Int}                     end_time 结束时间（选填）
     * @apiSuccessExample 成功响应
     * {
    "code": 200,
    "data": {
    "download_url": "asdasdasd.com"
    }
     */
    public function exportExcel(Request $request)
    {
        $param = $request->only(['start_time', 'end_time']);
        $va = app('validator')->make($param, [
            'start_time' => 'integer|min:1',//开始时间
            'end_time'   => 'integer|min:1',//结束时间
        ]);
        if ($va->fails()) {
            throw  new BasicException(10001, $va->errors()->first());
        }
        $list = empty($where) ? app('model_account_remind')->orderBy('created_at', 'desc')->get() : app('model_account_remind')->orderBy('created_at', 'desc')->where($where)->get();
        $list = empty($list) ? [] : $list->toArray();
        $res = [];

        foreach ($list as $k => $v) {
            $data = $this->dealData(json_decode($v['origin_data'], true));
            //            $res[$k] = $data;
            $res[$k][0] = $v['created_at'];
            $res[$k][1] = $data['all_user_money'];
            $res[$k][2] = $data['all_user_earnings'];
            $res[$k][3] = $data['all_user_gift'];
            $res[$k][4] = $data['all_labor_earnings'];
            $res[$k][5] = $data['all_labor_money'];
            $res[$k][6] = $data['all_water_money'];
            $res[$k][7] = $data['all_game_money'] ?? 0;
        }

        $headings = [
            '纪录时间',
            '总余额',
            '总收益',
            '背包礼物',
            '工会收益',

            '工会余额',
            '水滴价值',
            '霍克游戏价值',

        ];

        return app('repo_export')->export(19, $request->all(), $headings, $res);

    }

    private function dealData($data)
    {
        $res = [];
        foreach ($data as $key => $value) {
            switch ($value['type']) {
                case 1:
                    $res['all_user_money'] = $value['money'];//平台所有用户总余额
                    break;
                case 2:
                    $res['all_user_earnings'] = $value['money'];//平台所有用户总收益
                    break;
                case 3:
                    $res['all_user_gift'] = $value['money'];//平台所有用户背包礼物价值
                    break;
                case 4:
                    $res['all_labor_earnings'] = $value['money'];//公会收益
                    break;
                case 5:
                    $res['all_water_money'] = $value['money'];//总水滴价值
                    break;
                case 6:
                    $res['all_game_money'] = $value['money'];//霍克游戏钻石价值金币数
                    break;
                case 7:
                    $res['all_check_money'] = $value['money'];//提现审核中金币数
                    break;
                case 8:
                    $res['all_labor_money'] = $value['money'];//公会余额
                    break;
                default:
                    break;
            };
        }

        return $res;
    }

}
